package main

import (
	"fmt"
	"sync"
	"time"
)

var signal = make(chan struct{})

func fun1() {
	var a = []int{1, 2, 3, 4, 5}
	var wg sync.WaitGroup
	for _, v := range a { // v变量仅被声明了一次在后续的整个迭代过程会被重复使用
		wg.Add(1)
		go func() {
			time.Sleep(time.Second)
			fmt.Println(v)
			wg.Done()
		}()
	}
	wg.Wait()

	signal <- struct{}{}

}

func fun2() {
	<-signal
	var a = []int{1, 2, 3, 4, 5}
	var wg sync.WaitGroup
	for _, v := range a {
		wg.Add(1)
		go func(v int) {
			time.Sleep(time.Second)
			fmt.Println(v)
			wg.Done()
		}(v) // 传入的是 v的副本，值拷贝
	}
	wg.Wait()
}

func main() {

	go fun1()
	go fun2()

	for {

	}

}

/*
5
5
5
5
5
4
3
5
2
1

*/
